﻿Imports System.Data.SqlClient
Public Class Frm_NhanVien

    Dim clsdm As New Cls_DanhMuc
    Function KiemTraTextBox() As Integer
        Dim str As String = ""
        If Me.txttennv.Text.Trim = "" Then
            str += "Chưa có Loại SP nào ? & Chr(13)"
        End If
        If str <> "" Then
            MsgBox("Thông tin cân nhập:" & Chr(13) & Chr(13) & str, MsgBoxStyle.Information, "Thông Báo")
            Return 0
        Else
            Return 1
        End If
    End Function
    Private Sub Frm_NhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clsdm.FillDTG("select * from NhanVien", "NhanVien", Me.dtg_nv, Me.dts)
    End Sub
    Private Sub Getvalue(ByVal i As Integer)
        txtmanv.Text = dtg_nv.Item(i, 0)
        txttennv.Text = dtg_nv.Item(i, 1)
    End Sub
    Private Sub dtg_nv_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtg_nv.CurrentCellChanged
        Getvalue(dtg_nv.CurrentRowIndex)
    End Sub
#Region " thêm xoa sua "
    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click
        If Me.btthem.Text = "&Thêm" Then
            Me.btthem.Text = "&Hủy"
            Me.btluu.Enabled = True
            Me.btsua.Enabled = False
            Me.btxoa.Enabled = False
            Me.txttennv.Text = ""
            Me.txttennv.Focus()

        Else
            Me.btthem.Text = "&Thêm"
            Me.txttennv.Text = ""
            Me.btsua.Enabled = True
            Me.btxoa.Enabled = True

        End If
    End Sub
    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        If KiemTraTextBox() = 1 Then
            Dim cmd As New SqlCommand("ThemNV", con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@TenNV", SqlDbType.NVarChar).Value = txttennv.Text.Trim
            Try
                cmd.ExecuteNonQuery()
                MsgBox("Cập nhật thành công ", MsgBoxStyle.Information, "Thông Báo")

                Me.txttennv.Text = ""
            Catch ex As Exception
                MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
            End Try
        End If
        Me.btthem.Text = "&Thêm"
        Me.btluu.Enabled = False
        Me.btsua.Enabled = True
        Me.btxoa.Enabled = True
        clsdm.FillDataGird("select * from NhanVien", Me.dtg_nv, Me.dts)
    End Sub
    Private Sub btxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btxoa.Click
        If KiemTraTextBox() = 1 Then
            Dim strma, strten As String
            strma = dtg_nv.Item(dtg_nv.CurrentCell.RowNumber, 0)
            strten = dtg_nv.Item(dtg_nv.CurrentCell.RowNumber, 1)
            If MsgBox("Bạn có chắc xóa Nhân Viên : " & strten & "?", MsgBoxStyle.YesNo, "Thông Báo") = MsgBoxResult.Yes Then
                Try
                    Dim cmd As New SqlCommand("XoaNV", con)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.Add("@MaNV", SqlDbType.Int).Value = Me.txtmanv.Text.Trim
                    cmd.ExecuteNonQuery()
                    Me.txttennv.Text = ""
                Catch ex As Exception
                    MsgBox("Lỗi khi xóa" & ex.Message)
                    Exit Sub
                End Try
            End If
        End If
        clsdm.FillDataGird("select * from NhanVien", Me.dtg_nv, Me.dts)


    End Sub
    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsua.Click
        If KiemTraTextBox() = 1 Then
            Try
                Dim cmd As New SqlCommand("SuaNV", con)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@Manv", SqlDbType.Int).Value = Me.txtmanv.Text.Trim
                cmd.Parameters.Add("@Tennv", SqlDbType.NVarChar).Value = Me.txttennv.Text.Trim
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox("Lỗi khi Sửa" & ex.Message)
                Exit Sub
            End Try
        End If
        clsdm.FillDataGird("select * from NhanVien", Me.dtg_nv, Me.dts)
    End Sub
#End Region
    Private Sub btthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthoat.Click
        Me.Close()
    End Sub


End Class